using B.S.Data.MES.API.Applications.BaseData.Command.BOMCommand;
using B.S.Domain.BasicData.MaterialManagement.BOM;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.BOMCommandHandlers
{
    public class DeleteBOMCommandHandler : IRequestHandler<DeleteBOMCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<BOM> _bomRepository;

        public DeleteBOMCommandHandler(IBaseRepository<BOM> bomRepository)
        {
            _bomRepository = bomRepository;
        }

        public async Task<APIRequst<int>> Handle(DeleteBOMCommand request, CancellationToken cancellationToken)
        {
            var existingBOM = _bomRepository.GetAll().FirstOrDefault(x => x.BomId == request.BomId && !x.isDel);
            existingBOM.isDel = true;
            existingBOM.DeleteName = request.DeleteName;
            existingBOM.DeleteTime=DateTime.Now;
            await _bomRepository.Update(existingBOM);
            return new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString()
            };
        }
    }
}